package com.oocl.ha.merchant.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oocl.ha.dao.OrderDao; 
import com.oocl.ha.entity.Order;
import com.oocl.ha.merchant.service.OrderManager;

@Service("mom")
@Transactional
public class OrderManagerImpl implements OrderManager {
	@Autowired
	private OrderDao od; 
	

	public List<Order> loadAllOrder(int mId) {
		return od.loadAllOrder(mId);
	}

	
	public List<Order> findNotTakenOrders(int mId) {
		List<Order> orders = od.loadAllOrder(mId);
		List<Order> notTakens = new ArrayList<Order>();
		if (orders!=null) {
			for(Order o:orders){
				if(o.getTaken()==0){
					notTakens.add(o);
				}
			}
					
		}
		return notTakens;	
	}

	public List<Integer> findNotTakensOid(int mId) {
		List<Order> orders = od.loadAllOrder(mId);
		List<Integer> oIds = new ArrayList<Integer>();
		for(Order o:orders){
			if(o.getTaken()==0){
				oIds.add(o.getOid());
			}
		}
		return oIds;
	}

	public Order loadOrder(int oId) {
		return od.findOrderById(oId);
	}


	public int addOrder(Order order, int mid, int uid) {
		// TODO Auto-generated method stub
		return od.addOrder(order, mid, uid);
	}


	public int updateOrderTaken(int oid, int taken) {
		// TODO Auto-generated method stub
		return od.updateOrderTaken(oid, taken);
	}


	public Order findOrderById(int oid) {
		// TODO Auto-generated method stub
		return od.findOrderById(oid);
	}


	public List<Order> findOrderByUid(int uid) {
		// TODO Auto-generated method stub
		return od.findOrderByUid(uid);
	}


	public List<Order> findHistoryOrder(int mId) {
		List<Order> orders = new ArrayList<Order>();
		List<Order> allOrder=od.loadAllOrder(mId);
		if (allOrder!=null) {
			for(Order o:od.loadAllOrder(mId)){
				if(o.getTaken()!=0){
					orders.add(o);
				}
			}			
		}
		return orders;
	}


	public List<Order> findTakenOrders(int mId) {
		List<Order> orders = new ArrayList<Order>();
		List<Order> allOrder=od.loadAllOrder(mId);
		if (allOrder!=null) {
			for(Order o:allOrder){
				if(o.getTaken()==1){
					orders.add(o);
				}
			}			
		}
		return orders;
	}


	public List<Order> findOrderDoNotTake(int mId) {
		List<Order> orders = new ArrayList<Order>();
		List<Order> allOrder=od.loadAllOrder(mId);
		if (allOrder!=null) {
			for(Order o:allOrder){
				if(o.getTaken()==-2){
					orders.add(o);
				}
			}			
		}
		return orders;
	} 
	
	
}
